package com.gimi.cloud.bbp.manage.controller.mall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.mall.MallAppBannerDTO;
import com.gimi.cloud.bbp.manage.service.mall.MallAppBannerGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppBannerService;
import com.github.pagehelper.PageInfo;

/**
 * banner相关controller
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-30
 **/

@RestController
@RequestMapping("/mall/banner")
public class MallBannerController {
    private static final Logger logger = LoggerFactory.getLogger(MallBannerController.class);

    private MallAppBannerGroupService mallAppBannerGroupService;
    private MallAppBannerService mallAppBannerService;

    @Autowired
    public MallBannerController(MallAppBannerGroupService mallAppBannerGroupService, MallAppBannerService mallAppBannerService) {
        this.mallAppBannerGroupService = mallAppBannerGroupService;
        this.mallAppBannerService = mallAppBannerService;
    }

    /**
     * 获取Banner列表
     * @param request
     * @param response
     * @param dto 查询参数
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response,@RequestBody MallAppBannerDTO dto) throws Exception{
        Response resp = new Response();
        List<MallAppBannerDTO> banner = mallAppBannerService.queryList(dto,true);
        PageInfo<MallAppBannerDTO> pageInfo = new PageInfo<>(banner);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,pageInfo);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 获取单一的Banner信息
     * @param request
     * @param response
     * @param dto
     * @throws Exception
     */
    @RequestMapping("/info")
    public Response info(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppBannerDTO dto) throws Exception{
        Response resp = new Response();
        MallAppBannerDTO appBanner = mallAppBannerService.load(dto,dto.getAbgId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("appBanner",appBanner);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        return resp;
    }

    /**
     * 新增或编辑banner信息
     * @param request
     * @param response
     * @param dto 待保存的数据
     * @throws Exception
     */
    @RequestMapping("/saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request, HttpServletResponse response,@RequestBody MallAppBannerDTO dto) throws Exception{
        Response resp = new Response();
        mallAppBannerService.saveOrUpdate(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"更新成功");
        return resp;
    }
    /**
     * 删除banner数据
     * @param request
     * @param response
     * @param dto 待删除的banner组数据
     * @throws Exception
     */
    @RequestMapping("/delete")
    private Response delete(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppBannerDTO dto) throws  Exception{
        Response resp = new Response();
        mallAppBannerService.delete(dto,dto.getAbrId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"删除成功");
        return resp;
    }
}
